<?php
header("content-type:text/html;charset:utf-8");
/*粗糙的限制来源
$url = $_SERVER["HTTP_REFERER"];   //获取完整的来路URL
$str = str_replace("http://","",$url);  //去掉http://
$strdomain = explode("/",$str);               // 以“/”分开成数组
$domain = $strdomain[0];              //取第一个“/”以前的字符
if($domain!=="允许的来源"){
    echo 'Error2';
    exit;
}
*/
/*自己的入库
require_once '../class/mysql.class.php';
$mysql = new MySQL('localhost','***','***','***');
*/
function curl_put($cfile){
    $target="https://sm.ms/api/upload";
    $imgdata = array('smfile' => $cfile);
    $curl = curl_init();
    curl_setopt($curl, CURLOPT_URL, $target);
    curl_setopt($curl, CURLOPT_USERAGENT,'Opera/9.80 (Windows NT 6.2; Win64; x64) Presto/2.12.388 Version/12.15');
    curl_setopt($curl, CURLOPT_HTTPHEADER,array('User-Agent: Opera/9.80 (Windows NT 6.2; Win64; x64) Presto/2.12.388 Version/12.15','Referer: http://someaddress.tld','Content-Type: multipart/form-data'));
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); 
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); 
    curl_setopt($curl, CURLOPT_POST, true);
    curl_setopt($curl, CURLOPT_POSTFIELDS, $imgdata);
    curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
    $r = curl_exec($curl); 
    curl_close($curl);
    return $r;
}
if ((($_FILES["img"]["type"] == "image/gif")
|| ($_FILES["img"]["type"] == "image/jpeg")
|| ($_FILES["img"]["type"] == "image/x-icon")
|| ($_FILES["img"]["type"] == "image/png")))
{
    $cfile = curl_file_create($_FILES["img"]["tmp_name"],$_FILES["img"]["type"],$_FILES["img"]["name"]);
    $data = curl_put($cfile);
    $json = json_decode($data,true);
	print_r($json);
	/*
    $width = $json['data']['width'];
    $height = $json['data']['height'];
    $filename = $json['data']['filename'];
    $storename = $json['data']['storename'];
    $size = $json['data']['size'];
    $url = $json['data']['url'];
    $delete = $json['data']['delete'];
    $user_id = '1';//$_POST['user_id'];
    $album = '1';//$_POST['album'];
    $mysql->data(array('width'=>$width,
    'height'=>$height,
    'filename'=>$filename,
    'storename'=>$storename,
    'size'=>$size,'url'=>$url,
    'delete'=>$delete,
    'user_id'=>$user_id,
    'album'=>$album))->table('img_info')->add();
    //echo "<script>alert('上传成功!');window.opener=null;window.open('','_self');window.close();</script>";
    echo $url;*/
    exit;
}else{
    echo 'Error1';
    exit;
}
?>